﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.Windows.Controls.Ribbon;

namespace ScrumMagic
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class ProjectListForm : ScrumMagic.CommonForm
    {
     

        // test ribbon window
        private ProjectListRibbon productListRibbon;
        //current user Id
        public int USER_ID = 1;
        SqlCommand com;

        public override RibbonTab RibbonContextualTab
        {
          get 
          {
            productListRibbon.ribbonMenu.Tabs.Remove(productListRibbon.tabEdit);
            return productListRibbon.tabEdit;
          }
        }

        public ProjectListForm()
        {
            InitializeComponent();

            productListRibbon = new ProjectListRibbon();
            productListRibbon.FindButtonClicked += new EventHandler<TextEventArgs>(OnMenuFindButtonClicked);
            productListRibbon.tabEdit.Label = "Project List";
            productListRibbon.AddButtonClicked += new EventHandler(OnMenuAddButtonClicked);
            try
            {
                com = DBController.MainConnection.CreateCommand();
                com.CommandText =
                    "select Product.Id, Product.Name, [Version].Name AS Version, Project.Name AS Project, [Status].Name AS Status, Project.Comment " +
                    "from Product left join [Version] on Product.Version_Id = [Version].Id " +
                    "inner join Project on Product.Project_Id = Project.Id " +
                    "inner join Status on Product.Status_Id = Status.Id " +
                    "where Product.id in (select product_Id from RoleInProduct where User_Id=" + USER_ID + ");";
                com.CommandType = System.Data.CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter(com);
                DataTable table = new DataTable();
                adapter.Fill(table);
                dataGrid1.ItemsSource = table.DefaultView;
                adapter.Dispose();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't get table Project",
                  "ScrumMagic",
                  MessageBoxButton.OK);
            }

        }

        void OnMenuAddButtonClicked(object sender, EventArgs e)
        {
            AddTab addTab = new AddTab();
            FormController.OpenForm(addTab, FormController.ContainerTypes.tab);
        }

        void OnMenuFindButtonClicked(object sender, TextEventArgs e)
        {
            DataView dataSourse = dataGrid1.ItemsSource as DataView;
            dataSourse.RowFilter = string.Format("Name LIKE '{0}%'", e.Text);
        }

        private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataRowView row = (DataRowView)dataGrid1.SelectedItem;
            ProjectCardForm card = new ProjectCardForm(row[0].ToString());
            FormController.OpenForm(card, FormController.ContainerTypes.window);
            
        }
    }
}
